﻿using _03_Tank.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03_Tank
{
    enum Tag
    {
        MyTank, EnemyTank
    }

    internal class Bullet : Movething
    {
        public Tag Tag { get; set; }

        public bool IsDestory { get; set; }

        public Bullet(int x, int y, int speed, Direction dir, Tag tag)
        {
            BitmapDown = Resources.BulletDown;
            BitmapUp = Resources.BulletUp;
            BitmapLeft = Resources.BulletLeft;
            BitmapRight = Resources.BulletRight;

            IsDestory = false;
            X = x;
            Y = y;
            Speed = speed;
            Dir = dir;
            Tag = tag;

            this.X -= Width / 2;
            this.Y -= Height / 2;
        }

        public override void DrawSelf()
        {

            base.DrawSelf();
        }

        public override void Update()
        {
            MoveCheck();
            Move();
            base.Update();
        }

        private void MoveCheck()
        {
            if (Dir == Direction.Up)
            {
                if (Y + Height/2 + 3 < 0)
                {
                    IsDestory = true; return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Height/2 - 3 > 450)
                {
                    IsDestory = true; return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X + Width/2 - 3 < 0)
                {
                    IsDestory = true; return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Width/2 + 3 > 450)
                {
                    IsDestory = true; return;
                }
            }


            Rectangle rectangle = GetRectangle();
            rectangle.X = X + Width / 2 - 3;
            rectangle.Y = Y + Height / 2 - 3;
            rectangle.Height = 3;
            rectangle.Width = 3;

            int xExplosion = this.X + Width / 2;
            int yExplosion = this.Y + Height / 2;

            NotMovething wall = null;
            if ((wall = GameObjectManager.IsCollidedWall(rectangle)) != null)
            {
                IsDestory = true;
                GameObjectManager.DestoryWall(wall);
                GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                return;
            }

            if (GameObjectManager.IsCollidedSteel(rectangle) != null)
            {
                IsDestory = true;
                return;
            }

            if (GameObjectManager.IsCollidedBoss(rectangle))
            {
                GameFramework.ChangeToGameOver();
                return;
            }

            if (Tag == Tag.MyTank)
            {
                EnemyTank tank = null;
                if ((tank = GameObjectManager.IsCollidedEnemyTank(rectangle)) != null)
                {
                    IsDestory = true;
                    GameObjectManager.DestoryTank(tank);
                    return;
                }
            }
            else if (Tag == Tag.EnemyTank)
            {
                MyTank myTank = null;
                if ((myTank = GameObjectManager.IsCollidedMyTank(rectangle)) != null)
                {
                    IsDestory = true;
                    GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                    myTank.TakeDamage();
                }
            }
        }

        private void Move()
        {
            //if (!IsMoving) return;

            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                default:
                    break;
            }
        }
    }
}
